package com.own.demo.lwf.common.list;

import com.own.demo.lwf.entity.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @Description: 1.foreach遍历
 * 2.sort默认按年龄升序排序
 * 3.sort自定义按年龄降序序排序
 * 4.filter过滤
 * 5.map也为过滤但过滤的结果变为过滤条件
 * 6. max/min按条件返回最大/最小值
 * 7.distinct()去重
 * 8.limit返回前n个元素
 * 9.skip跳过前n个元素
 * 10.parallelStream并行流和forEach快速遍历
 * @Author Administrator
 * @Date 2022/8/12 0012 22:01
 * @copyright 李文峰
 */
public class StreamDemo {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<>();

        Student s1 = new Student(3, "zhangsan", "beijing", 16, 90.0);
        list.add(s1);

        Student s2 = new Student(2, "lisi", "shanghai", 19, 80.8);
        list.add(s2);

        Student s3 = new Student(1, "lining", "shandong", 20, 70.8);
        list.add(s3);

        Student s4 = new Student(3, "fanbingbing", "beijing", 23, 70.0);
        list.add(s4);

        Student s5 = new Student(2, "libingbing", "shanghai", 30, 95.0);
        list.add(s5);

        Student s6 = new Student(3, "wangwu", "shanghai", 39, 85.0);
        list.add(s6);

        Student s7 = new Student(3, "wangwu", "shanghai", 39, 85.0);
        list.add(s7);

        // 1.foreach遍历
        list.stream().forEach(System.out::println);

        // 2.sort默认按年龄升序排序
        List<Student> sortList = list.stream().sorted((a, b) -> a.getAge().compareTo(b.getAge())).collect(Collectors.toList());
        sortList.stream().forEach(System.out::println);


        // 3.sort自定义按年龄降序序排序
        List<Student> sortList2 = list.stream().sorted(Comparator.comparingInt(Student::getAge).reversed()).collect(Collectors.toList());
        sortList2.stream().forEach(System.out::println);

        // 4.filter过滤
        List<Student> sortList3 = list.stream().filter(Student -> Student.getAge() > 20).collect(Collectors.toList());
        sortList3.stream().forEach(System.out::println);

        // 5.map也为过滤但过滤的结果变为过滤条件
        List<Integer> integerList = list.stream().map(student -> student.getAge()).collect(Collectors.toList());
        integerList.stream().forEach(System.out::println);

        //6. max/min 按条件返回最大/最小值
        Student student = list.stream().max(Comparator.comparing(Student::getAge)).get();
        System.out.println(student);

        // 7.distinct()去重
        List<Student> studentList = list.stream().distinct().collect(Collectors.toList());
        studentList.stream().forEach(System.out::println);

        // 8.limit返回前n个元素
        List<Student> studentList1 = list.stream().limit(3).collect(Collectors.toList());
        studentList1.stream().forEach(System.out::println);

        // 9.skip跳过前n个元素
        List<Student> studentList2 = list.stream().skip(5).collect(Collectors.toList());
        studentList2.stream().forEach(System.out::println);

        // 10.parallelStream并行流和forEach快速遍历
        List<Student> studentList3 = list.parallelStream().collect(Collectors.toList());
        studentList3.forEach(System.out::println);

    }
}
